package com.salts.controller;

import com.salts.domain.dto.CarNumUpdateDto;
import com.salts.domain.dto.ConvertOrderDto;
import com.salts.domain.dto.OrderDeliverDto;
import com.salts.domain.dto.Result;
import com.salts.domain.po.UserAddress;
import com.salts.domain.vo.CarVo;
import com.salts.service.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 购物车模块 /car
 */
@RestController
@RequestMapping("/car")
public class ShoppingCarController {
    @Autowired
    ShoppingCarService shoppingCarService;

    /**
     * 添加商品到购物车接口 /add -Post
     */
    @RequestMapping("/add")
    public Result addProductInCar(@RequestBody OrderDeliverDto orderDeliverDto){
         Result result = shoppingCarService.addProductInCar(orderDeliverDto);
         return result;
    }//添加商品到购物车接口

    /**
     * 按用户查看购物车列表接口 返回结果为<日期:结果> /queryList -Post
     */
    @RequestMapping("/queryList")
    public Result queryCarList(@RequestBody Map<String, Integer> requestBody){
        Integer userId = requestBody.get("userId");
        Map<String, List<CarVo>> stringListMap = shoppingCarService.queryCarList(userId);
        if (stringListMap==null){
            return Result.ok(null);
        }
        List<Map> returnList = new ArrayList<>();
        for (Map.Entry<String, List<CarVo>> stringListEntry : stringListMap.entrySet()) {
            HashMap<String, Object> car = new HashMap<>();
            car.put("createTime",stringListEntry.getKey());
            car.put("goodsList",stringListEntry.getValue());
            returnList.add(car);
        }
        return Result.ok(returnList);
    }//按天查看购物车列表接口

    /**
     * 修改购物车中商品数量接口 /updateNum -Post
     */
    @RequestMapping("/updateNum")
    public Result updateCarNum(@RequestBody CarNumUpdateDto carNumUpdateDto){
     Integer id = carNumUpdateDto.getId();
     Integer number = carNumUpdateDto.getNumber();
        Result result = shoppingCarService.updateCarNum(id, number);
        return result;
    }//修改购物车中商品数量接口

    /**
     * 删除购物车中商品接口 /deleteOne -Post
     * @param OrderDeliverDto：
     * @param Integer userId; 用户id
     * @param Integer clothId; 服装id
     * @param Integer specId; 尺寸id
     * @param Integer colorId; 颜色id
     * @return {
     *     "success": true,
     *     "errorMsg": null,
     *     "data": "删除成功",
     *     "total": null
     * }
     */
    @RequestMapping("/deleteOne")
    public Result deleteCarInfo(@RequestBody CarNumUpdateDto carNumUpdateDto){
        Integer id = carNumUpdateDto.getId();
        Result result = shoppingCarService.deleteCarInfoOne(id);
        return result;
    }//删除购物车中商品接口

    /**
     * 清空用户购物车 /deleteAll -Post
     * @param userId
     * @return Data>0则成功
     */
    @RequestMapping("/deleteAll")
    public Result deleteCarInfo(@RequestBody Map<String,Integer> paraMap){
        Integer userId = paraMap.get("userId");
        Result result = shoppingCarService.deleteCarInfo(userId);
        return result;
    }
    /**
     * 购物车结算接口（生成订单） /checkoutCar -Post
     */
    @RequestMapping("/checkoutCar")
    public Result checkoutCar(@RequestBody ConvertOrderDto convertOrderDto){
         List<CarVo> carVoList = convertOrderDto.getCarVoList();
         UserAddress userAddress = convertOrderDto.getUserAddress();
         String remark = convertOrderDto.getRemark();
         int orderType = convertOrderDto.getOrderType();
         Result result = shoppingCarService.checkoutCar(carVoList, userAddress, remark, orderType);
         return result;
    }//购物车结算接口

    /**
     * 购物车选中状态修改接口  /updateFlag -Post
     * @param carVo 购物车对象
     * @return {
     *     "success": true,
     *     "errorMsg": null,
     *     "data": true,
     *     "total": null
     * }
     */
    @RequestMapping("/updateFlag")
    public Result updateIsFlag(@RequestBody CarVo carVo)//购物车选中状态修改接口
    {
        Result result = shoppingCarService.updateIsFlag(carVo);
        return result;
    }
}
